#!/usr/bin/env python
# -*- coding: utf-8 -*-



from subprocess import call
import os, inspect, sys, time

def which(program):

    def is_exe(fpath):
        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
    fpath, fname = os.path.split(program)
    if fpath:
        if is_exe(program):
            return program
    else:
        for path in os.environ["PATH"].split(os.pathsep):
            exe_file = os.path.join(path, program)
            if is_exe(exe_file):
                return exe_file

    return None


def install_networkx():
    curr_path = inspect.currentframe().f_code.co_filename
    dir_path  = os.path.dirname(curr_path)
    nx_path = os.path.join(dir_path,'packages','networkx-1.7')
    command ='python setup.py install'
    os_resp = call(command, shell=True, cwd=nx_path)    
    return 

    
def install_dot2tex():  
    curr_path = inspect.currentframe().f_code.co_filename
    dir_path  = os.path.dirname(curr_path)
    d2t_path = os.path.join(dir_path,'packages','dot2tex-2.8.7')
    command ='python setup.py install'
    os_resp = call(command, shell=True, cwd=d2t_path)
    return

def install_pyparsing():  
    curr_path = inspect.currentframe().f_code.co_filename
    dir_path  = os.path.dirname(curr_path)
    d2t_path = os.path.join(dir_path,'packages','pyparsing-1.5.6')
    command ='python setup.py install'
    os_resp = call(command, shell=True, cwd=d2t_path)
    return 

def install_pydot():  
    curr_path = inspect.currentframe().f_code.co_filename
    dir_path  = os.path.dirname(curr_path)
    d2t_path = os.path.join(dir_path,'packages','pydot-1.0.28')
    command ='python setup.py install'
    os_resp = call(command, shell=True, cwd=d2t_path)
    return 

def test_networkx():
    """ Testing networkx dependence """
    try:
        import networkx
        from networkx.version import version_info 
        curr_version = version_info[1:3]
        if  curr_version >= (1,5):
            ver = str(curr_version[0])+'.'+str(curr_version[1])    
            test_nx ="version %s of Networkx found, and it is valid"%ver
        else:
            test_nx= "version %s of Networkx found, and it will be installed version 1.7"%ver
            install_networkx()
    except ImportError:
        install_networkx()
        test_nx=" I am installing Networkx v1.7"
    return test_nx



def test_dot2tex():
    """ Testing dot2tex dependence """
    try:
        import dot2tex
        from dot2tex.dot2tex import  __version__ 
        curr_ver = __version__.split('.')
        curr_ver = (int(curr_ver[0]), int(curr_ver[1]))
        if  curr_ver >= (2,5):       
            print >> sys.stderr, "version %s of dot2tex found, and it is valid"%__version__
        else:            
            install_dot2tex()  
            print >> sys.stderr,"I am installing dot2tex v2.8"      
    except ImportError:
        install_dot2tex()
    return


def test_pyparsing():
    """ Testing pyparsing dependence """
    try:
        import pyparsing
        from pyparsing import  __version__ 
        curr_ver = __version__.split('.')
        curr_ver = (int(curr_ver[0]), int(curr_ver[1]))
        if  curr_ver >= (1,0):       
            test_pp= "version %s of pyparsing found, and it is valid."%__version__
        else:
            install_pyparsing()        
            test_pp= "I am installing pyparsing 1.5.6"
    except ImportError:
        install_pyparsing()
        test_pp= "I am installing pyparsing 1.5.6"
    return test_pp




def test_pydot():
    """ Testing pydot dependence """
    try:
        import pydot
        from pydot import  __version__ 
        curr_ver = __version__.split('.')
        curr_ver = (int(curr_ver[0]), int(curr_ver[1]))
        if  curr_ver >= (1,0):       
            test_pd= "version %s of pydot found, and it is valid"%__version__
        else:
            test_pd="I am installing pydot v1.0.28" 
            install_pydot()      
             
    except ImportError:
        install_pydot()
        test_pd="I am installing pydot v1.0.28" 
    return test_pd

def test_graphviz():
    """ Testing graphviz dependence """
    if  which('dot.exe'):       
        test_gr='- Graphviz is correctly installed'
    else:
        test_gr='- Graphviz was not yet installed. Please install it for using graphical functions'         
    return test_gr
    

def test_pdflatex():            
    """ 
    Testing latex dependence
    """
    if  which('pdflatex.exe'):
        test_pdf = '- pdflatex is correctly installed'
    else:
        test_pdf =  '- latex was not yet installed. Please install it to use graphical functions' 
        
    return test_pdf





print >> sys.stderr,""
print >> sys.stderr,"I am revising executable programs:"
print >> sys.stderr,"--------------------------------------------------------------"
print >> sys.stderr,""
print >> sys.stderr,""
print >> sys.stderr, test_graphviz()
print >> sys.stderr, test_pdflatex()
print >> sys.stderr,""
print >> sys.stderr,"I am revising and installing required python packages:"
print >> sys.stderr,"-----------------------------------------------------------------------------------"
print >> sys.stderr,""
print >> sys.stderr, test_networkx()
print >> sys.stderr, test_pyparsing()
print >> sys.stderr, test_pydot()
print >> sys.stderr,""
print >> sys.stderr, "I just completed  DESlab installation."
print >> sys.stderr,"--------------------------------------------------------------"
print >> sys.stderr, "Thanks for your interest in this program."








